<template>
	<view class="content">
		<view class="top">
			<view class="title">姓名：</view>
			<input v-model="params.agentName" placeholder="请输入姓名" class="input" @input="inputs"/>
			<view class="button" @click="search">
				<uni-icons type="search" size="18" color="#fff" class="icons" />
				搜 索
			</view>
		</view>
		<view class="structure">
			<view class="ul">
				<view class="li" v-for="(item, index) in agentlist" :key="index">
					<view class="msg" >
						<image src="/static/img/bottom.png" @click.stop="openExpand(item)" :class="{ avtive:item.isExpand}" v-if="item.children"/>
						<text class="name"  :class="current === item.id ? 'current' : ''" @click="choose(item)">{{item.name}} <text>{{item.roleName}}</text> </text>
					</view>
					<view class="sublevel" v-if="item.children.length>0 && item.isExpand">
						<view class="list" v-for="(item1, index1) in item.children" :key="index1">
							<view class="msg" >
								<image src="/static/img/bottom.png" @click.stop="openExpand(item1)" :class="{ avtive:item1.isExpand}" v-if="item1.children"/>
								<text class="name" :class="current === item1.id ? 'current' : ''" @click="choose(item1)">{{item1.name}} <text>{{item1.roleName}}</text> </text>
							</view>
							<view class="sublevel" v-if="item1.children.length>0 && item1.isExpand">
								<view class="list" v-for="(item2, index2) in item1.children" :key="index2">
									<view class="msg" >										
										<image src="/static/img/bottom.png" @click.stop="openExpand(item2)" :class="{ avtive:item2.isExpand}" v-if="item2.children"/><text class="name" :class="current === item2.id ? 'current' : ''" @click="choose(item2)">{{item2.name}} <text>{{item2.roleName}}</text> </text>
									</view>
									<view class="sublevel" v-if="item2.children.length>0 && item2.isExpand">
										<view class="list" v-for="(item3, index3) in item2.children" :key="index3">
											<view class="msg" >										
												<text class="name" :class="current === item3.id ? 'current' : ''" @click="choose(item3)">{{item3.name}} <text>{{item3.roleName}}</text> </text>
											</view>
										</view>
										
									</view>
								</view>
								
							</view>
						</view>
						
					</view>
				</view>
			</view>
			<view class="choice" @click="close">确认选择</view>
		</view>
	</view>
</template>

<script>
	import particulars from '../../../servers/agent/placeorder/particulars.js';
	export default {
		data() {
			return {
				agentlist:[],
				params:{
					agentName:''
				},
				current:'',
				items:'',
				type:''
			}
		},
		onLoad(option) {
			this.type=option.type;
			console.log(this.type)
			this.open();
		},
		mounted() {
			// this.getPersonList();
		},
		methods: {
			//获取代理商数据
			handleTreeData(treeData, searchValue) {
			    if (!treeData || treeData.length === 0) {
			      return [];
			    }
			    const array = [];
			    for (let i = 0; i < treeData.length; i += 1) {
					 console.log(this.handleTreeData(treeData[i].children, '赵'))
			      if (this.handleTreeData(treeData[i].children, searchValue).length > 0 || treeData[i].name.includes(searchValue)) {
			        array.push({
			          ...treeData[i],
			          children: this.handleTreeData(treeData[i].children, searchValue),
			        });
			      }
			    }
			    return array;
			  },
			  inputs(){
				if(!this.params.agentName){
					this.open();
				}  
			  },
			  search(){
				  if(this.handleTreeData(this.agentlist,this.params.agentName).length>0){
					  this.agentlist=this.handleTreeData(this.agentlist,this.params.agentName).map((v,j)=>{
					  	if (v.isExpand) {
					  		this.$set(v, 'isExpand', v.isExpand)
					  	} else {
					  		this.$set(v, 'isExpand', false)
					  	}
					  	if(v.children){
					  		v.children = v.children.map((m, i) => {
					  			// m.isExpand = m.isExpand || false
					  			m.children = m.children || []
					  			if (m.isExpand) {
					  				this.$set(m, 'isExpand', m.isExpand)
					  			} else {
					  				this.$set(m, 'isExpand', false)
					  			}
					  			
					  			if (m.children.length > 0) {
					  				
					  				m.children = m.children.map((m, i) => {
					  					// m.isExpand = m.isExpand || false
					  					m.children = m.children || []									
					  					if (m.isExpand) {
					  						this.$set(m, 'isExpand', m.isExpand)
					  					} else {
					  						this.$set(m, 'isExpand', false)
					  					}
					  					if(m.children.length > 0){
					  						m.children = m.children.map((m, i) => {
					  							// m.isExpand = m.isExpand || false
					  							m.children = m.children || []									
					  							if (m.isExpand) {
					  								this.$set(m, 'isExpand', m.isExpand)
					  							} else {
					  								this.$set(m, 'isExpand', false)
					  							}
					  							if(m.children.length > 0){
					  								
					  							}
					  							return m;
					  						})
					  					}
					  					return m;
					  				})
					  			}
					  			return m;
					  		})
					  	}
					  	
					  	return v;
					  })
				  }else{
					  uni.showToast({
					  	title:'没有您要找的信息',
					  	icon:'none'
					  })
				  }
				 
				 
			  },
			openExpand(item) {
				item.isExpand=!item.isExpand	
			
			},
			choose(item){
				
				this.current=item.id;
				this.items=item	
			},
			close(){
				// uni.redirectTo({
				// 	url:'./particulars?item='+JSON.stringify(this.items) 
				// })
				uni.$emit('checked', JSON.stringify(this.items))
				uni.navigateBack({
					delta:1
				})
			},
			// 检查代理商是否可用
			open() {
				
				uni.showLoading({
				    title: '数据加载中'
				});
					var tradeFlag=''
					// 代理商结构树
					
					this.type==1?tradeFlag='':tradeFlag=1
					console.log(tradeFlag)
					particulars.searchagent({'tradeFlag':tradeFlag}).then(res => {
						 console.log(res.data)
						if(res.code==200){
							// 需要在 popup 组件，指定 ref 为 popup
							
							if(res.data){
								this.agentlist=res.data;
								
								this.agentlist=res.data.map((v,j)=>{
									if (v.isExpand) {
										this.$set(v, 'isExpand', v.isExpand)
									} else {
										this.$set(v, 'isExpand', false)
									}
									if(v.children){
										v.children = v.children.map((m, i) => {
											// m.isExpand = m.isExpand || false
											m.children = m.children || []
											if (m.isExpand) {
												this.$set(m, 'isExpand', m.isExpand)
											} else {
												this.$set(m, 'isExpand', false)
											}
											
											if (m.children.length > 0) {
												
												m.children = m.children.map((m, i) => {
													// m.isExpand = m.isExpand || false
													m.children = m.children || []									
													if (m.isExpand) {
														this.$set(m, 'isExpand', m.isExpand)
													} else {
														this.$set(m, 'isExpand', false)
													}
													if(m.children.length > 0){
														m.children = m.children.map((m, i) => {
															// m.isExpand = m.isExpand || false
															m.children = m.children || []									
															if (m.isExpand) {
																this.$set(m, 'isExpand', m.isExpand)
															} else {
																this.$set(m, 'isExpand', false)
															}
															if(m.children.length > 0){
																
															}
															return m;
														})
													}
													return m;
												})
											}
											return m;
										})
									}
									
									return v;
								})
								console.log(this.agentlist)
								
									
							}else{
								uni.showToast({
									title:'没有您要找的信息',
									icon:'none'
								})
							}
							
						}else{
							uni.showToast({
								title:res.msg,
								icon:'none'
							})
						}
							uni.hideLoading();
							
						})
						.catch(err => {
							console.log(err)
							console.log(new Error('请检查网络后重试'));
						});	
				
			},
		}
	}
</script>

<style scoped lang="scss" rel="stylesheet/scss">
	.current{
		color: #bd3c8c;
	}
	.top {
		background: #ffffff;
		padding: 46rpx 20rpx;
		display: flex;
		flex-direction: row;
		line-height: 84rpx;
	
		.title {
			font-size: 30rpx;
		}
	
		.input {
			width: 430rpx;
			border-radius: 10rpx;
			box-shadow: 5rpx 0px 20rpx #c5c5c5;
			margin: 0 27rpx 0 18rpx;
			padding: 0 20rpx;
			box-sizing: border-box;
			font-size: 24rpx;
			height: 84rpx;
		}
	
		.button {
			height: 84rpx;
			line-height: 84rpx;
			padding: 0rpx 20rpx;
			text-align: center;
			color: #ffffff;
			border-radius: 10rpx;
			font-size: 28rpx;
			background: #bd3c8c;
			display: flex;
			align-items: center;
		}
	}
	.structure {
		width: 90vw;
		max-height: 60vh;
		margin: 0 auto;
		overflow-y: auto;
		background: #ffffff;
		border-radius: 20rpx;
		.ul {
			padding-bottom: 100rpx;
			.li {
				line-height: 70rpx;
				.msg {
					font-size: 30rpx;
					
					image {
						width: 26rpx;
						height: 22rpx;
						margin-right: 26rpx;
						transform: rotate(-90deg);
					}
					image.avtive {
						transform: rotate(0deg);
					}
					.name {
						margin-right: 30rpx;
						text{
							margin-left: 20rpx;
							color: #999;
						}
					}
					.isname {
						color: #bd3c8c;
					}
				}
				.sublevel {
					padding: 0 20rpx 0 60rpx;
				}
			}
		}
		.choice {
			position: fixed;
			bottom: 50rpx;
			width: 90%;
			line-height: 88rpx;
			background: #bd3c8c;
			color: #fff;
			text-align: center;
			font-size: 30rpx;
			border-radius:  10rpx;
		}
	}
</style>
